#!/usr/bin/python3

url = 'http://mirror.centos.org/centos/7/os/x86_64/Packages'
url = 'http://mirrors.neusoft.edu.cn/centos/7/os/x86_64/Packages'

import requests
import re
import sys
import time
import wget

def get_rpms(url):
    headers = {'Connection': 'keep-alive','pgrade-Insecure-Requests': '1','ser-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66'}
    html = requests.get(url, headers=headers)
    if html.status_code == 200:
        # print( html.text)
        rpms = re.findall('<a href=".*.rpm">(.*.rpm)</a>', html.text)
    return rpms

def downloads(url, files):
    for file in files:
        downloadurl = url+'/'+file
        print("\n"+downloadurl)
        # download(downloadurl, file)
        download_by_wget(downloadurl)

def download(url,savefile):
    urlfile = requests.get(url)
    if urlfile.status_code == 200:
        open(file, 'wb').write(urlfile.content)

def download_by_wget(url):
    file_name = wget.download(url)

rpms = get_rpms(url)
print('获取到[%d]个RPM包,开始下载任务' % len(rpms))
downloads(url, rpms)
print('下载任务完成')